package lab03.test06;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Test06 {
    public static void main(String[] args) {

        String path = "D:\\java实验\\lab0306.txt";

        RandomAccessFile file = null;
        Map<String, Integer> map = new HashMap<String, Integer>();
        List<Map.Entry<String, Integer>> List = null;

        try {
            file = new RandomAccessFile(path, "r");

            String line = null;
            int count = 0;
            while ((line = file.readLine()) != null) {

                //单词个数
                System.out.print(line + " ");
                count = count + 1;

                //相同单词的个数，并按照单词出现频率大小打印
                if (map.get(line) == null) {
                    map.put(line, 1);
                } else {
                    int value = map.get(line);
                    value = value + 1;
                    map.put(line, value);
                    map.put(line, map.get(line));
                }
            }
            System.out.println();
            for (Map.Entry<String, Integer> entry : map.entrySet()) {
                System.out.print(entry.getKey() + " " + entry.getValue() + "  ");
            }

            System.out.println();
            System.out.println("总共出现了" + count + "个英文单词");
            System.out.println("总共有：" + map.size() + "个不同的单词");

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                file.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
